<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="其他配置信息id" prop="matchId">
      <el-input v-model="dataForm.matchId" placeholder="其他配置信息id"></el-input>
    </el-form-item> -->
    <el-form-item label="文章类型" prop="articleType">
      <el-select v-model="dataForm.articleType">
        <el-select v-model="dataForm.articleType" placeholder="请选择" clearable>
          <el-option v-for="item in wxArticleTypeOptions" :key="item.id" :label="item.typeName" :value="item.id"></el-option>
        </el-select>
      </el-select>
    </el-form-item>
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="文章描述" prop="articleDescribes">
      <el-input v-model="dataForm.articleDescribes" placeholder="文章描述"></el-input>
    </el-form-item>
    <el-form-item label="图片id" prop="imageId">
      <el-input v-model="dataForm.imageId" placeholder="图片id"></el-input>
    </el-form-item>
    <el-form-item label="内容体是否显示" prop="contentShow">
      <el-radio-group v-model="dataForm.contentShow">
        <el-radio :label="1">是</el-radio>
        <el-radio :label="0">否</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="内容"></el-input>
    </el-form-item>
    <el-form-item label="启用状态" prop="status">
      <el-radio-group v-model="dataForm.status">
        <el-radio :label="1">启用</el-radio>
        <el-radio :label="0">停用</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="发布标识" prop="release">
      <el-radio-group v-model="dataForm.release">
        <el-radio :label="1">发布</el-radio>
        <el-radio :label="0">不发布</el-radio>
      </el-radio-group>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getWxArticleObj, addWxArticleObj, putWxArticleObj} from '@/api/mp/wxarticle'
    import { getWxArticleTypeList} from '@/api/mp/wxarticletype'
    export default {
    data () {
      return {
        visible: false,
        wxArticleTypeOptions: [],
        dataForm: {
          id: 0,
          matchId: '',
          title: '',
          articleType: '',
          articleDescribes: '',
          imageId: '',
          contentShow: 0,
          content: '',
          status: 1,
          release: 0,
          createTime: '',
          createUser: '',
          updateTime: '',
          updateUser: '',
          delFlag: '',
          tenantId: ''
        },
        dataRule: {
          matchId: [
            { required: true, message: '其他配置信息id不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          articleType: [
            { required: true, message: '文章类型不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '启用状态(1-启用,0-停用)不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          updateUser: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '删除标识(1-未删除,-1已删除)不能为空', trigger: 'blur' }
          ],
          tenantId: [
            { required: true, message: '租户Id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getArticleType()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getWxArticleObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putWxArticleObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                addWxArticleObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      },
      //获取文章类型
      getArticleType(){
        getWxArticleTypeList().then(result => {
          this.wxArticleTypeOptions = result.data.data
        })
      }
    }
  }
</script>
